﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace RecipeRecommender
{
    class MealTypeUtil
    {
        public static List<MealType> MealTypeList = new List<MealType>();
        // Mapping of name to MealType object
        public static Dictionary<string, MealType> MealTypeMap = new Dictionary<string, MealType>();

        public static double[,] MealTypeSimilarityTable = new double[0, 0];

        private const string FILE_NAME1 = "CBRData\\MealTypeList.txt";
        private const string FILE_NAME2 = "CBRData\\MealTypeSimilarityTable.txt";

        public static void setAllMealTypes()
        {

            if (!File.Exists(FILE_NAME1))
            {
                Console.WriteLine("{0} does not exist.", FILE_NAME1);
            }
            using (StreamReader sr = File.OpenText(FILE_NAME1))
            {
                String input;
                int count = 0;
                while ((input = sr.ReadLine()) != null)
                {
                    MealType cm = new MealType(count, input.Trim());
                    MealTypeList.Add(cm);
                    MealTypeMap.Add(cm.Name, cm);
                    count++;
                }
                Console.WriteLine("The end of the stream has been reached.");

            }

            int size = MealTypeList.Count;
            MealTypeSimilarityTable = new double[size, size];

            if (!File.Exists(FILE_NAME2))
            {
                Console.WriteLine("{0} does not exist.", FILE_NAME2);
            }
            using (StreamReader sr = File.OpenText(FILE_NAME2))
            {
                String input;
                int row = 0;
                while ((input = sr.ReadLine()) != null)
                {
                    int column = 0;
                    string[] words = input.Split('\t');
                    foreach (string word in words)
                    {
                        MealTypeSimilarityTable[row, column] = Convert.ToDouble(word);
                        column++;
                    }
                    row++;
                }
                Console.WriteLine("The end of the stream has been reached.");

            }


            // Print Array
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    Console.Write(MealTypeSimilarityTable[i, j] + "\t");
                }
                Console.WriteLine();
            }

        }

        public static MealType getMealTypeByName(string name)
        {
            if (MealTypeMap.ContainsKey(name))
                return MealTypeMap[name];
            else return null;
        }

        public static double getSimilarityScore(MealType input, MealType examined)
        {
            if (input == null) return 0;
            else return MealTypeSimilarityTable[input.Index, examined.Index];
        }
    }
}
